package com.appbee.songs.client.ui.entry;

import com.allen_sauer.gwt.dnd.client.HasDragHandle;
import com.appbee.gdata.youtube.client.model.SimpleEntry;
import com.appbee.gdata.youtube.client.ui.IYoutubeStyle;
import com.google.gwt.core.client.GWT;
import com.google.gwt.i18n.client.NumberFormat;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.Image;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.Widget;

public class EntryView extends Composite implements HasDragHandle
{

	private static EntryViewUiBinder uiBinder = GWT
			.create(EntryViewUiBinder.class);

	interface EntryViewUiBinder extends UiBinder<Widget, EntryView>
	{
	}

	@UiField
	Label dragHandle;

	@UiField
	Image thumbnail;

	@UiField
	Label text;

	@UiField
	Label duration;

	@UiField
	Button removeButton;

	private SimpleEntry entry;

	public EntryView(SimpleEntry entry, IYoutubeStyle youtubeStyle)
	{
		initWidget(uiBinder.createAndBindUi(this));
		setStyleName(youtubeStyle.entryWrap());
		thumbnail.setStyleName(youtubeStyle.media());
		text.setStyleName(youtubeStyle.entryContent());
		duration.setStyleName(youtubeStyle.entryDuration());
		removeButton.setStyleName(youtubeStyle.deleteButton());
		dragHandle.setStyleName(youtubeStyle.dragHandle());
		setEntry(entry);
	}

	public void setEntry(SimpleEntry entry)
	{
		this.entry = entry;
		thumbnail.setUrl(entry.getThumbnail());
		text.setText(entry.getTitle());
		int d = entry.getDuration();
		int m = d / 60;
		int s = d % 60;
		String durationText = m + ":" + NumberFormat.getFormat("00").format(s);
		duration.setText(durationText);

	}

	public SimpleEntry getEntry()
	{
		return entry;
	}

	public Button getRemoveButton()
	{
		return removeButton;
	}

	@Override
	public Widget getDragHandle()
	{
		return dragHandle;
	}

}
